import java.sql.SQLException;
import java.time.LocalDate;
import java.util.List;

public class BillService {
    private BillDAO billDAO;

    public BillService(BillDAO billDAO) {
        this.billDAO = billDAO;
    }

    // 根据日期查询账单
    public List<Bill> queryBills(String date) throws SQLException {
        return billDAO.queryBillsByDate(date);
    }

    // 设置月度预算
    public void setMonthlyBudget(double budget) throws SQLException {
        if (budget <= 0) {
            throw new IllegalArgumentException("预算金额必须大于0");
        }
        int currentMonth = LocalDate.now().getMonthValue();
        billDAO.setMonthlyBudget(currentMonth, budget);
    }

    // 获取月度统计报告
    public String getMonthlyReport(int month) throws SQLException {
        if (month < 1 || month > 12) {
            throw new IllegalArgumentException("月份必须在1-12之间");
        }
        MonthlyReport report = billDAO.getMonthlyReport(month);
        return report.toString();
    }

    // 添加记录收入方法
    public void recordIncome(String date, double amount, String category, String note) throws SQLException {
        if (amount <= 0) {
            throw new IllegalArgumentException("金额必须大于0");
        }
        Bill bill = new Bill(date, amount, category, note, "收入");
        billDAO.insertBill(bill);
    }

    // 添加记录支出方法
    public void recordExpense(String date, double amount, String category, String note) throws SQLException {
        if (amount <= 0) {
            throw new IllegalArgumentException("金额必须大于0");
        }
        Bill bill = new Bill(date, amount, category, note, "支出");
        billDAO.insertBill(bill);
    }

    // 添加获取所有账单方法
    public List<Bill> getAllBills() throws SQLException {
        return billDAO.getAllBills();
    }
}